package com.online.music.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.online.music.entity.po.Comment;
import com.online.music.entity.po.CommentReport;
import com.online.music.entity.vo.CommentVo;
import com.online.music.mapper.CommentMapper;
import com.online.music.mapper.CommentReportMapper;
import com.online.music.service.ICommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import lombok.RequiredArgsConstructor;

import java.util.List;

import com.online.music.common.Result;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Author: Leno
 * @Date: 2025-01-31
 * @Description: CommentServiceImpl
 */
@Service
@RequiredArgsConstructor
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements ICommentService {

    private final CommentMapper commentMapper;

    private final CommentReportMapper commentReportMapper;

    @Override
    public Result<Page<CommentVo>> findPage(Integer pageNum, Integer pageSize, Comment comment) {
        Page<Comment> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Comment> qw = new QueryWrapper<>();
        // 添加条件...

        if (StrUtil.isNotBlank(comment.getContent())) {
            qw.like("c.content", comment.getContent());
        }

        if (comment.getSongId() != null) {
            qw.eq("c.song_id", comment.getSongId());
        }

        Page<CommentVo> result = commentMapper.findPage(page, qw);

        List<CommentVo> records = result.getRecords();
        if (!records.isEmpty()){
            // 查询举报信息
            List<CommentReport> commentReports = commentReportMapper.selectList(
                    new LambdaQueryWrapper<CommentReport>()
                            .in(CommentReport::getCommentId, result.getRecords().stream().map(CommentVo::getId).toList())
            );
            records.forEach(commentVo -> commentVo.setCommentReports(commentReports));
            return Result.success(result);
        }
        return Result.success(result);
    }

    @Override
    @Transactional
    public Result<String> deleteBatch(List<Long> ids) {
        // 删除前先判断是否存在...
        removeByIds(ids);
        return Result.successMsg("删除成功");
    }

}

